<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page errorPage="../jsp/index.jsp?site=error"%>
<jsp:useBean id="loginBean" class="de.fhhof.vinf.ssp.studienarbeit11.beans.LoginBean" scope="session"></jsp:useBean>
<%
	String email = request.getParameter("email");
	String password = request.getParameter("password");
	loginBean.setEmail(email);
	loginBean.setPassword(password);
	loginBean.setErrors(new ArrayList<String>());
	if(loginBean.isValidLogin()){
		Statement stmt;
		ResultSet res;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/studienarbeit";
			Connection con = DriverManager.getConnection(url, "admin", "cisco");
			stmt = con.createStatement();
			try {
				res = stmt.executeQuery("SELECT user_id FROM users WHERE email='" + email +"';");
				Integer userId = null;
				while (res.next()){
					userId = res.getInt("user_id");
				}
				if(userId != null ){
					session.setAttribute("user_id", userId);
					%><jsp:forward page="<%= response.encodeURL(\"../jsp/index.jsp?site=profile\")%>"></jsp:forward><%
				}
			} catch (SQLException e) {
				con.close();
			} finally {
				con.close();
			}
		} catch (Exception e) {
			
		}
	} else {
		session.setAttribute("login_errors", loginBean.getErrors());
		%><jsp:forward page="<%= response.encodeURL(\"../jsp/index.jsp\") %>"></jsp:forward><%
	}
%>